<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="全网总存储量" prop="capacity"  v-if="columns[0].visible"/>
          <el-table-column label="等候中" prop="waitCount"  v-if="columns[1].visible"/>
          <el-table-column label="担保人" prop="guarantorCount"  v-if="columns[2].visible"/>
          <el-table-column label="全网产币量" prop="output"  v-if="columns[3].visible"/>
          <el-table-column label="奖励量" prop="reward"  v-if="columns[4].visible"/>
          <el-table-column label="创建时间" align="center" prop="createTime"  v-if="columns[5].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="全网总存储量(PB)" prop="capacity">
              <el-input v-model="form.capacity"  placeholder="请输入全网总存储量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等候中" prop="waitCount">
              <el-input v-model="form.waitCount"  placeholder="请输入等候人数"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="担保人" prop="guarantorCount">
              <el-input v-model="form.guarantorCount"  placeholder="请输入担保人数"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全网产币量(CRU)" prop="output">
              <el-input v-model="form.output"  placeholder="请输入全网产币量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="奖励量" prop="reward">
              <el-input v-model="form.reward"  placeholder="请输入奖励量"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {poolDataList,  poolDataAdd} from "@/api/biz"
import {nonnegative } from '@/utils/validate'

export default {
  name: "PoolData",
  data() {
    const validFormNonnegative = (rule, value, callback) => {
      if (!nonnegative(value)) {
        callback(new Error("请输入非负整数"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        id:'',
        capacity: '',  //全网总存储量
        waitCount:'',  //等候中
        guarantorCount:'',  //担保人
        output:'',  //全网产币量
        reward:''  //奖励量
      },
      // 列信息
      columns: [
        { key: 0, label: `全网总存储量`, visible: true },
        { key: 1, label: `等候中`, visible: true },
        { key: 2, label: `担保人`, visible: true },
        { key: 3, label: `全网产币量`, visible: true },
        { key: 4, label: `奖励量`, visible: true },
        { key: 5, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {
        capacity: [
          { required: true, message: "请输入全网总存储量", trigger: "blur" },
          { required: true, validator: validFormNonnegative, trigger: "blur" }
        ],
        waitCount: [
          { required: true, message: "请输入等候中人数", trigger: "blur" },
          { required: true, validator: validFormNonnegative, trigger: "blur" }
        ],
        guarantorCount: [
          { required: true, message: "请输入担保人数", trigger: "blur" },
          { required: true, validator: validFormNonnegative, trigger: "blur" }
        ],
        output: [
          { required: true, message: "请输入全网产币量", trigger: "blur" },
          { required: true, validator: validFormNonnegative, trigger: "blur" }
        ],
        reward: [
          { required: true, message: "请输入奖励量", trigger: "blur" },
          { required: true, validator: validFormNonnegative, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增矿池信息";
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      poolDataList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id:'',
        capacity: '',  //全网总存储量
        waitCount:'',  //等候中
        guarantorCount:'',  //担保人
        output:'',  //全网产币量
        reward:''  //奖励量
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          poolDataAdd(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
<style  lang="scss">
.tableImageStyle{
  width: 30px;
  vertical-align: middle;
}
.ant-upload-preview {
  background-color: #fff;
  .avatar-uploader {
    .upload-desc {
      position: absolute;
      bottom: 0;
      left: 0;
      color: #ccc;
      font-size: 8px;
      right: 0;
    }
  }
}
::v-deep .avatar-uploader .el-upload--text {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  margin-right: 20px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 22px;
  color: #ccc;
  width: 108px;
  height: 108px;
  line-height: 108px;
  text-align: center;
}
::v-deep.avatar {
  width: 108px;
  height: 108px;
  display: block;
}
</style>
